package com.lanyam.travel.base.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lanyam.travel.base.dao.GoodsInfoMapper;
import com.lanyam.travel.base.dao.ScenicHotelMapper;
import com.lanyam.travel.base.entity.GoodsInfo;
import com.lanyam.travel.base.entity.HomeProduct;
import com.lanyam.travel.base.entity.Product;
import com.lanyam.travel.base.entity.ScenicHotel;
import com.lanyam.travel.base.entity.SupplierInfo;
import com.lanyam.travel.base.entity.TravelLine;
import com.lanyam.travel.base.service.IGoodsInfoService;
import com.lanyam.travel.base.service.IScenicHotelService;
import com.lanyam.travel.base.service.ISupplierInfoService;

@Service
public class ScenicHotelServiceImpl implements IScenicHotelService {

	@Autowired
	private ScenicHotelMapper scenicHotelMapper;
	
	@Autowired
	private IGoodsInfoService goodsInfoService;
	
	@Autowired
	private ISupplierInfoService supplierInfoService;
	
	@Override
	public List<ScenicHotel> getAllScenicHotel() {
		// TODO Auto-generated method stub
		List<ScenicHotel> selectList = scenicHotelMapper.selectList(null);
		return selectList;
	}

	@Override
	public void deleteById(String productId) {
		// TODO Auto-generated method stub
		scenicHotelMapper.deleteById(productId);
		//如果有就删掉(酒店有，景点没有)
		goodsInfoService.deleteByProductId(productId);
	}

	@Override
	public void update(ScenicHotel scenicHotel) {
		// TODO Auto-generated method stub
		scenicHotelMapper.updateById(scenicHotel);
	}

	@Override
	public void addScenic(ScenicHotel scenicHotel) {
		// TODO Auto-generated method stub
		scenicHotelMapper.insert(scenicHotel);
	}

	@Override
	public ScenicHotel queryScenicById(String id) {
		// TODO Auto-generated method stub
		ScenicHotel scenic = scenicHotelMapper.selectById(id);
		List<GoodsInfo> goodsInfoList = goodsInfoService.getGoodsInfoByProductId(id);
		scenic.setGoodsInfoList(goodsInfoList);
		return scenic;
	}
	/**
	 *	 根据条件查出景点列表并分页返回
	 * @param pageNum 第几页
	 * @param pageSize 每页的个数
	 * @param reqMap 请求参数：
	 * price:价格排序，值=0代表升序排列（默认），1代表降序排列
	 * region:类型，分国内，国外，值=0代表国内，1代表国外，-1不区分国内外（默认）
	 * distance:距离排序，值=0代表升序排列（默认），1代表降序排列
	 * location:地区搜索
	 * 	star:星级搜索
	 * 	commentScore:评分排序， 值=0代表升序排列，1代表降序排列（默认）
	 * product_type:产品类型，0为景点，1为酒店
	 * @return
	 */
	@Override
	public Page<ScenicHotel> queryScenicListPage(int pageNum, int pageSize, Map<String, Object> reqMap) {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> queryWrapper = new QueryWrapper<ScenicHotel>();
		queryWrapper.eq("status", 1);
		String region = reqMap.get("region")!=null ? reqMap.get("region").toString():"";
		String location = reqMap.get("location")!=null ? reqMap.get("location").toString():"";
		String sortByPrice = reqMap.get("price")!=null ? reqMap.get("price").toString():"";
		String star = reqMap.get("star")!=null ? reqMap.get("star").toString():"";
		String sortByCommentScore = reqMap.get("commentScore")!=null ? reqMap.get("commentScore").toString():"";
		String productType = reqMap.get("product_type")!=null ? reqMap.get("product_type").toString():"";
		queryWrapper.eq("product_type",productType);
		if(!StringUtils.isEmpty(region))
		{
			queryWrapper.eq("region",region);
		}
		if(!StringUtils.isEmpty(star))
		{
			queryWrapper.eq("star",star);	
		}
		if(!StringUtils.isEmpty(location))
		{
			queryWrapper.like("location",location);
		}
		if(!StringUtils.isEmpty(sortByPrice))
		{
			if("0".equals(sortByPrice))
			{
				queryWrapper.orderByAsc("price");	
			}
			else
			{
				queryWrapper.orderByDesc("price");
			}
		}
		if(!StringUtils.isEmpty(sortByCommentScore))
		{
			if("0".equals(sortByCommentScore))
			{
				queryWrapper.orderByAsc("comment_score");	
			}
			else
			{
				queryWrapper.orderByDesc("comment_score");
			}
		}
		Page<ScenicHotel> page = new Page<ScenicHotel>(pageNum,pageSize);
		Page<ScenicHotel> selectPage = scenicHotelMapper.selectPage(page, queryWrapper);
		
		return selectPage;
	}

	@Override
	public List<ScenicHotel> getScenicByMap(Map<String, Object> reqMap) {
		// TODO Auto-generated method stub
		String name = (String) reqMap.get("name");
    	QueryWrapper<ScenicHotel> wrapper = new QueryWrapper<ScenicHotel>();
    	wrapper.like("name", name);
		return scenicHotelMapper.selectList(wrapper);
	}

	@Override
	public List<ScenicHotel> batchQueryScenics(List<String> scenicIds) {
		// TODO Auto-generated method stub
		return scenicHotelMapper.selectBatchIds(scenicIds);
	}

	@Override
	public ScenicHotel getScenicById(String scenicId) {
		// TODO Auto-generated method stub
		return scenicHotelMapper.selectById(scenicId);
	}

	@Override
	public Page<ScenicHotel> queryScenicPage(int pageNum, int pageSize, Map<String, String> reqMap) {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> queryWrapper = new QueryWrapper<ScenicHotel>();
		if(null == reqMap)
		{
			queryWrapper = null;
		}
		else
		{
			String name = reqMap.get("name");
			if(!StringUtils.isEmpty(name))
			{
				queryWrapper.like("NAME", name);
			}
			String region = reqMap.get("region");
			if(!StringUtils.isEmpty(region))
			{
				queryWrapper.eq("region", region);
			}
			String status = reqMap.get("status");
			if(!StringUtils.isEmpty(status))
			{
				queryWrapper.eq("status", status);
			}
			String productType = reqMap.get("productType");
			if(!StringUtils.isEmpty(productType))
			{
				queryWrapper.eq("product_type", productType);
			}
			String location = reqMap.get("location");
			if(!StringUtils.isEmpty(location))
			{
				queryWrapper.like("location", location);
			}
			String star = reqMap.get("star");
			if(!StringUtils.isEmpty(star))
			{
				queryWrapper.eq("star", star);
			}
			String lowPrice = reqMap.get("lowPrice");
			String highPrice = reqMap.get("highPrice");
			if(!StringUtils.isEmpty(lowPrice) && !StringUtils.isEmpty(highPrice))
			{
				queryWrapper.between("price", lowPrice,highPrice);
			}
			else if(!StringUtils.isEmpty(lowPrice) && StringUtils.isEmpty(highPrice))
			{
				queryWrapper.ge("price", lowPrice);
			}
			else if(StringUtils.isEmpty(lowPrice) && !StringUtils.isEmpty(highPrice))
			{
				queryWrapper.lt("price", highPrice);
			}
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
				String startTime = reqMap.get("startDate");
				String endTime = reqMap.get("endDate");
				Date startDate = null;
				if(!StringUtils.isEmpty(startTime))
				{
					startDate = sdf.parse(startTime);
				}
				Date endDate = null;
				if(!StringUtils.isEmpty(endTime))
				{
					endDate = sdf.parse(endTime);
				}
				
				if(null != startDate && null != endDate)
				{
					queryWrapper.between("create_time", startDate,endDate);
				}
				else if(null != startDate && null == endDate)
				{
					queryWrapper.ge("create_time", startDate);
				}
				else if(null == startDate && null != endDate)
				{
					queryWrapper.lt("create_time", endDate);
				}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		List<SupplierInfo> allSupplier = supplierInfoService.getAllSupplier();
		Map<String,SupplierInfo> supplierMap = new HashMap<String, SupplierInfo>();
		for (SupplierInfo supplierInfo : allSupplier) {
			supplierMap.put(supplierInfo.getId(), supplierInfo);
		}
		Page<ScenicHotel> page = new Page<ScenicHotel>(pageNum,pageSize);
		Page<ScenicHotel> ipage = scenicHotelMapper.selectPage(page, queryWrapper);
		List<ScenicHotel> scenicList = ipage.getRecords();
		SupplierInfo info;
		for (ScenicHotel scenicHotel : scenicList) {
			List<GoodsInfo> infoList = goodsInfoService.getGoodsInfoByProductId(scenicHotel.getId());
			scenicHotel.setGoodsInfoList(infoList);
			info = supplierMap.get(scenicHotel.getSupplyId());
			if(null != info)
			{
				scenicHotel.setSupplierName(info.getSupplierName());
			}
		}
		return ipage;
	}

	@Override
	public List<? extends Product> selectList(QueryWrapper<ScenicHotel> scenicWrapper) {
		// TODO Auto-generated method stub
		List<ScenicHotel> selectList = scenicHotelMapper.selectList(scenicWrapper);
		return selectList;
	}

	@Override
	public List<ScenicHotel> searchByKey(String key) {
		// TODO Auto-generated method stub
		
		return null;
	}

	@Override
	public List<ScenicHotel> getAllHotels() {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> wrapper = new QueryWrapper<ScenicHotel>();
		wrapper.eq("product_type", 1);
		List<ScenicHotel> selectList = scenicHotelMapper.selectList(wrapper);
		return selectList;
	}

	@Override
	public List<ScenicHotel> getAllScenic() {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> wrapper = new QueryWrapper<ScenicHotel>();
		wrapper.eq("product_type", 0);
		List<ScenicHotel> selectList = scenicHotelMapper.selectList(wrapper);
		return selectList;
	}

	@Override
	public List<ScenicHotel> getProductBySupplier(String supplierId) {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> wrapper = new QueryWrapper<ScenicHotel>();
		wrapper.eq("supply_id", supplierId);
		List<ScenicHotel> scenicList = scenicHotelMapper.selectList(wrapper);
		return scenicList;
	}

	@Override
	public List<ScenicHotel> getProductBySupplierAndType(String supplierId, int productType) {
		// TODO Auto-generated method stub
		QueryWrapper<ScenicHotel> wrapper = new QueryWrapper<ScenicHotel>();
		wrapper.eq("supply_id", supplierId);
		wrapper.eq("product_type", productType);
		List<ScenicHotel> scenicList = scenicHotelMapper.selectList(wrapper);
		return scenicList;
	}

}
